package com.nativetalk.service.authentication.insert;

import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.authentication.TdNtAuthenticationInsert;
import com.nativetalk.bean.authentication.TdNtAuthenticationInsertTwo;
import com.nativetalk.bean.teacher.TdNtTeacherIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.oss.OssUtil;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.dao.TdNtAuthenticationDao;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 16/1/11.
 */
@Service
public class AuthenticationInsertServiceImpl implements AuthenticationInsertService {
    private static Logger logger = Logger.getLogger(AuthenticationInsertServiceImpl.class);
    @Resource
    private TdNtAuthenticationDao tdNtAuthenticationDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;

    @Override
    public RetInfo insertAuthentication(TdNtAuthenticationInsert tdNtAuthenticationInsert) throws IOException {
        String logInfo = this.getClass().getName() + ":insertAuthentication:";
        logger.info("======" + logInfo + "begin======");
        Date now = new Date();
        RetInfo retInfo = new RetInfo();
        try {
            TdNtTeacherIndex tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherIndex(tdNtAuthenticationInsert.getTea_id());
            if (tdNtTeacherIndex.getTea_islock().equals("3") || tdNtTeacherIndex.getTea_islock().equals("4")) {

                //上传声音简介
                CommonsMultipartFile synopsis = tdNtAuthenticationInsert.getSound_synopsis();
                if (synopsis != null && synopsis.getSize() > 31457280) {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("SYNOPSIS.SIZE.LARGE"));
                    return retInfo;
                } else {

                    ObjectMetadata objectMetadataSynopsis = new ObjectMetadata();
                    objectMetadataSynopsis.setContentLength(synopsis.getSize());
                    String synopsisUrl = Constants.FILE + "/" + Constants.FILEIMG + "/image/teacher/" + tdNtAuthenticationInsert.getTea_id() + "/synopsis/"
                            + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())) + "3" + synopsis.getOriginalFilename().substring(synopsis.getOriginalFilename().lastIndexOf("."));
                    OssUtil.uploadInputStream(synopsis.getInputStream(), objectMetadataSynopsis, synopsisUrl);
                    tdNtAuthenticationInsert.setTea_sound_synopsis(synopsisUrl);
                }

                //上传学生证
                CommonsMultipartFile studentId = tdNtAuthenticationInsert.getStudentId();
                if (studentId != null && studentId.getSize() > 31457280) {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("STUDENTID.SIZE.LARGE"));
                    return retInfo;
                } else {
                    ObjectMetadata objectMetadataStudent = new ObjectMetadata();
                    objectMetadataStudent.setContentLength(studentId.getSize());
                    String studentIdUrl = Constants.FILE + "/" + Constants.FILEIMG + "/image/teacher/" + tdNtAuthenticationInsert.getTea_id() + "/studentId/"
                            + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())) + "2" + studentId.getOriginalFilename().substring(studentId.getOriginalFilename().lastIndexOf("."));
                    OssUtil.uploadInputStream(studentId.getInputStream(), objectMetadataStudent, studentIdUrl);
                    tdNtAuthenticationInsert.setStudent_id(studentIdUrl);
                }
                //上传头像
                CommonsMultipartFile commonsMultipartFile = tdNtAuthenticationInsert.getHead_portrait();
                if (commonsMultipartFile != null && commonsMultipartFile.getSize() > 31457280) {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("HEAD.SIZE.LARGE"));
                    return retInfo;
                } else {

                    ObjectMetadata objectMetadata = new ObjectMetadata();
                    objectMetadata.setContentLength(commonsMultipartFile.getSize());
                    String headPortrait = Constants.FILE + "/" + Constants.FILEIMG + "/image/teacher/" + tdNtAuthenticationInsert.getTea_id() + "/head/"
                            + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())) + "1" + commonsMultipartFile.getOriginalFilename().substring(commonsMultipartFile.getOriginalFilename().lastIndexOf("."));
                    OssUtil.uploadInputStream(commonsMultipartFile.getInputStream(), objectMetadata, headPortrait);
                    tdNtAuthenticationInsert.setTea_head_portrait(headPortrait);

                }
                tdNtAuthenticationInsert.setAuh_month(CommonUtil.getMonth(now));
                tdNtAuthenticationInsert.setAuh_state("1");
                tdNtAuthenticationDao.insertTdNtAuthentication(tdNtAuthenticationInsert);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("添加申请成功");
            } else {
                /**
                 * 已认证
                 */
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("AUTHENT.RESULT.SUCCESS"));
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    @Transactional
    public RetInfo insertAuthenticationTwo(TdNtAuthenticationInsertTwo tdNtAuthenticationInsert) throws IOException {
        String logInfo = this.getClass().getName() + ":insertAuthenticationTwo:";
        logger.info("======" + logInfo + "begin======");
        Date now = new Date();
        RetInfo retInfo = new RetInfo();
        try {
            TdNtTeacherIndex tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherIndex(tdNtAuthenticationInsert.getTea_id());
            if (tdNtTeacherIndex.getTea_islock().equals("3") || tdNtTeacherIndex.getTea_islock().equals("4")) {
                //上传声音简介
                CommonsMultipartFile synopsis = tdNtAuthenticationInsert.getSound_synopsis();
                if (synopsis != null && synopsis.getSize() > 31457280) {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("SYNOPSIS.SIZE.LARGE"));
                    return retInfo;
                } else {

                    ObjectMetadata objectMetadataSynopsis = new ObjectMetadata();
                    objectMetadataSynopsis.setContentLength(synopsis.getSize());
                    String synopsisUrl = Constants.FILE + "/" + Constants.FILEIMG + "/image/teacher/" + tdNtAuthenticationInsert.getTea_id() + "/synopsis/"
                            + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())) + "3" + synopsis.getOriginalFilename().substring(synopsis.getOriginalFilename().lastIndexOf("."));
                    OssUtil.uploadInputStream(synopsis.getInputStream(), objectMetadataSynopsis, synopsisUrl);
                    tdNtAuthenticationInsert.setTea_sound_synopsis(synopsisUrl);
                }

                //上传学生证
                CommonsMultipartFile studentId = tdNtAuthenticationInsert.getStudentId();
                if (studentId != null && studentId.getSize() > 31457280) {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("STUDENTID.SIZE.LARGE"));
                    return retInfo;
                } else {
                    ObjectMetadata objectMetadataStudent = new ObjectMetadata();
                    objectMetadataStudent.setContentLength(studentId.getSize());
                    String studentIdUrl = Constants.FILE + "/" + Constants.FILEIMG + "/image/teacher/" + tdNtAuthenticationInsert.getTea_id() + "/studentId/"
                            + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())) + "2" + studentId.getOriginalFilename().substring(studentId.getOriginalFilename().lastIndexOf("."));
                    OssUtil.uploadInputStream(studentId.getInputStream(), objectMetadataStudent, studentIdUrl);
                    tdNtAuthenticationInsert.setStudent_id(studentIdUrl);
                }
                //上传头像
                CommonsMultipartFile commonsMultipartFile = tdNtAuthenticationInsert.getHead_portrait();
                if (commonsMultipartFile != null && commonsMultipartFile.getSize() > 31457280) {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("HEAD.SIZE.LARGE"));
                    return retInfo;
                } else {

                    ObjectMetadata objectMetadata = new ObjectMetadata();
                    objectMetadata.setContentLength(commonsMultipartFile.getSize());
                    String headPortrait = Constants.FILE + "/" + Constants.FILEIMG + "/image/teacher/" + tdNtAuthenticationInsert.getTea_id() + "/head/"
                            + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())) + "1" + commonsMultipartFile.getOriginalFilename().substring(commonsMultipartFile.getOriginalFilename().lastIndexOf("."));
                    OssUtil.uploadInputStream(commonsMultipartFile.getInputStream(), objectMetadata, headPortrait);
                    tdNtAuthenticationInsert.setTea_head_portrait(headPortrait);

                }

                tdNtAuthenticationInsert.setAuh_month(CommonUtil.getMonth(now));
                tdNtAuthenticationInsert.setAuh_state("1");
                TdNtAuthenticationInsert tdNtAuthentication = new TdNtAuthenticationInsert();
                tdNtAuthentication.setTea_head_portrait(tdNtAuthenticationInsert.getTea_head_portrait());
                tdNtAuthentication.setTea_id(tdNtAuthenticationInsert.getTea_id());
                tdNtAuthentication.setPayPal(tdNtAuthenticationInsert.getPayPal());
                tdNtAuthentication.setContact(tdNtAuthenticationInsert.getContact());
                tdNtAuthentication.setTea_sound_synopsis(tdNtAuthenticationInsert.getTea_sound_synopsis());
                tdNtAuthentication.setStudent_id(tdNtAuthenticationInsert.getStudent_id());
                tdNtAuthentication.setExam_month(tdNtAuthenticationInsert.getExam_month());
                tdNtAuthentication.setAuh_state(tdNtAuthenticationInsert.getAuh_state());
                Map<String, Object> map = new HashMap<>();
                map.put("tea_id", tdNtAuthenticationInsert.getTea_id());
                map.put("tea_name", tdNtAuthenticationInsert.getTea_name());
                map.put("tea_synopsis", tdNtAuthenticationInsert.getTea_synopsis());
                map.put("tea_sex", tdNtAuthenticationInsert.getTea_sex());
                map.put("dict_id", tdNtAuthenticationInsert.getDict_id());
                map.put("tea_islock", 2);
                map.put("pay_type", tdNtAuthenticationInsert.getPay_type());
                map.put("contact_type", tdNtAuthenticationInsert.getContact_type());
                tdNtTeacherDao.updateTdNtTeacherApply(map);
                tdNtAuthenticationDao.insertTdNtAuthentication(tdNtAuthentication);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("添加申请成功");

            } else {
                /**
                 * 已认证
                 */
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("AUTHENT.RESULT.SUCCESS"));
            }


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
